#!/bin/bash

#
#Bash script for parallel por compilation
#
# - It is used Java, JNI and C program for native access
# - Steps:
# 	- Compile the java class (javac MyClass.java -> output: MyClass.class)
#	- Generate the C stub    (javah MyClass -> output: MyClass.h)
#	- Compile the C code	 (gcc -shared -fPIC -o libMyClass.so -I JDK/include -I JDK/include/linux myclass.cpp
#	- Test the java program
#
# - Highlights:
#	- The JDK/include directory is where the Jni.h is located
#	- The java file will load the library using the patter lib*.so -> This is why it tries to load like: System.load("MwParallelPortLinux") = libMwParallelPortLinux.so.
#		- If this was made for windows, java would try to load MwParallelPortLinux.dll
#	- If the java class package changes, the .h stub and .cpp file should be re-generated
#		- In this case, re-generate the .h file, copy the method signature and paste in the .cpp
#	- There are two was for Java getting know the library location:
#		- LD_LIBRARY_PATH
#			- LD_LIBRARY_PATH=$(pwd) #Path to the lib.so
#			- export LD_LIBRARY_PATH
#			- ldconfig
#		- java.library.path
#			- java -Djava.library.path=lib.so Myclass

#JDK_HOME=/usr/lib64/jvm/java-openjdk
JDK_HOME=/usr/lib/jvm/java-6-openjdk-i386


echo "Cleaning directory"
rm *.so *.h
rm -Rf br

echo
echo "Generating .class"
$JDK_HOME/bin/javac ../src/br/com/multiway/parallel/MwParallelPortLinux.java -d .

echo
echo "Generating .h stubs" 
$JDK_HOME/bin/javah -jni -o Parallel.h br.com.multiway.parallel.MwParallelPortLinux

echo
echo "Compiling C code" 
gcc -shared -fPIC -o libMwParallelPortLinux.so -I $JDK_HOME/include/ -I $JDK_HOME/include/linux parallel.cpp

echo
echo "Testing java program"
java -Djava.library.path=. br.com.multiway.parallel.MwParallelPortLinux
